<?php
class User extends MY_Agent_Controller
{
    function __construct()
    {
        parent::__construct();
    }

    /**
     * 用户详情
     */
    public function profile()
    {
    	$this->load->model('agent/Model_agent','agent');
    	$agent_info = $this->agent->getOne(array('uid'=>$this->uInfo['uid']));
    	
    	$this->load->model('finance/Model_finance_user_capital_account', 'fuca');
    	$capital_account = $this->fuca->capital_account_by_uid($this->uInfo['uid'], CURRENCY_SGD);
    	$capital_account = $capital_account['data']['balance'];

        $data = array(
            'agent_info' => $agent_info,
            'capital_account' => $capital_account,
        );

        $this->load->view('agent/user/profile', $data);
    }
    
    /**
     * 用户详情
     */
    public function reset_password()
    {
    	$mobile   = $this->uInfo['mobile'];
    	$code     = trim($this->input->get_post('code'));
    	$password = trim($this->input->get_post('password'));
    	
    	if($code)
    	{    		
    		$this->load->service('user/service_user_password');
    		$data = $this->service_user_password->reset_password($mobile, $code, $password);
    		
    		$this->jump($data['code']);
    	}
    	
    	$this->load->view('agent/user/reset_password', array('info'=>$this->uInfo));
    }


    /**
     * 删除用户
     */
    public function delete()
    {
        $response = array('code' => '0', 'msg' => '删除成功');

        do {
            $uid = (int)$this->input->get_post('uid');
            $status = (int)$this->input->get_post('status');

            if (!$uid)
            {
                $response = error(100000);
                break;
            }


            $this->load->model('model_user', 'user');
            $item = $this->user->getOne(array('uid' => $uid));

            if (empty ($item))
            {
                $response = error(200002);
                break;
            }


            $userStatus = config_item('user_status');
            if (!array_key_exists($status, $userStatus))
                $status = USER_STATUS_DELETE;


            $res = $this->user->save(array('status' => $status), $uid);
            if (!$res)
            {
                $response = error(200003);
                break;
            }
        }while (false);


        $this->json_output($response);
    }


    /**
     * 用户登陆日志
     */
    public function login_log()
    {
        $isExport = $this->input->get_post('is_export');


        $limit = 20;
        $currentPage = (int)$this->input->get_post('per_page');
        $offset = ($currentPage < 1) ? 0 : ($currentPage - 1) * $limit;


        $uName  = $this->input->get_post('uname');
        $time   = $this->input->get_post('create_time');
        $source = $this->input->get_post('source');



        $where = array();
        $uName && $where['uname'] = $uName;
        ($source || $source === '0') && $where['source'] = $source;

        if ($time)
        {
            $eTime = explode('-', $time);//echo $eTime[0].'<br>';p(date('Y-m-d H:i:s', strtotime($eTime[0])));
            $where['create_time >='] = date('Y-m-d H:i:s', strtotime($eTime[0]));
            $where['create_time <='] = date('Y-m-d ', strtotime($eTime[1])).'23:59:59';
        }


        $this->load->model('Model_user_login_log', 'ull');

        $totalNum = $this->ull->count($where);
        $logInfo = $this->ull->lists($offset, $limit, $where);


        $this->load->library('pagination');
        $config = $this->page_config('User/login_log', $totalNum, $limit, http_build_query($where));
        $this->pagination->initialize($config);
        $pageHtml = $this->pagination->create_links();


        $this->load->model('model_user', 'user');
        $userData = $this->user->getUser(100000, 0, 'uname');


        $data = array(
            'title'    => '用户升级日志',
            'log_info' => $logInfo,
            'user_data'=> $userData,
            'pageHtml' => $pageHtml,
            'time'     => $time,
            'source'   => $source,
            'uname'    => $uName,
            'totalNum' => $totalNum,
        );

        if ($isExport) {
            $userStatus = config_item('user_status');
            $userSex = config_item('user_sex');
            $userSource = config_item('user_source');
            $str = "日志ID,用户ID,用户名,类别,当前等级,历史等级,描述,日志时间;\n";
            foreach ($logInfo as $v) {
                $str .= $v['log_id'].','.$v['uid'].','.$v['uname'].','.$v['type'].','.$v['current_level'].'级,'.$v['prev_level'].','.$v['descr'].','.$v['create_time'].";\n";
            }
            $fileName = 'user_up_log_'.date('Y-m-d', TIMESTAMP) .'.csv';
            exportCsv($fileName, $str);
            return;
        }

        $this->load->view('agent/user/login_log', $data);
    }


    /**
     * 用户升级日志
     */
    public function up_level()
    {
        $isExport = $this->input->get_post('is_export');


        $limit = 20;
        $currentPage = (int)$this->input->get_post('per_page');
        $offset = ($currentPage < 1) ? 0 : ($currentPage - 1) * $limit;


        $uName = $this->input->get_post('uname');
        $time = $this->input->get_post('create_time');
        $current_level = (int)$this->input->get_post('current_level');
        $type = $this->input->get_post('type');



        $where = array();
        $uName && $where['uname'] = $uName;
        $type  && $where['type'] = $type;
        $current_level && $where['current_level'] = $current_level;

        if ($time)
        {
            $eTime = explode('-', $time);//echo $eTime[0].'<br>';p(date('Y-m-d H:i:s', strtotime($eTime[0])));
            $where['create_time >='] = date('Y-m-d H:i:s', strtotime($eTime[0]));
            $where['create_time <='] = date('Y-m-d ', strtotime($eTime[1])).'23:59:59';
        }


        $this->load->model('Model_user_up_log', 'uul');

        $totalNum = $this->uul->count($where);
        $logInfo = $this->uul->lists($offset, $limit, $where);


        $this->load->library('pagination');
        $config = $this->page_config('User/up_level', $totalNum, $limit, http_build_query($where));
        $this->pagination->initialize($config);
        $pageHtml = $this->pagination->create_links();


        $this->load->model('model_user', 'user');
        $userData = $this->user->getUser(100000, 0, 'uname');


        $this->load->model('Model_user_level_config', 'ulc');
        $levelData = $this->ulc->lists(0, 100, array(), 'level');

        $data = array(
            'title'         => '用户升级日志',
            'log_info'      => $logInfo,
            'user_data'     => $userData,
            'pageHtml'      => $pageHtml,
            'time'          => $time,
            'type'          => $type,
            'uname'         => $uName,
            'current_level' => $current_level,
            'totalNum'      => $totalNum,
            'level_data'    => $levelData,
        );

        if ($isExport) {
            $userStatus = config_item('user_status');
            $userSex = config_item('user_sex');
            $userSource = config_item('user_source');
            $str = "日志ID,用户ID,用户名,类别,当前等级,历史等级,描述,日志时间;\n";
            foreach ($logInfo as $v) {
                $str .= $v['log_id'].','.$v['uid'].','.$v['uname'].','.$v['type'].','.$v['current_level'].'级,'.$v['prev_level'].','.$v['descr'].','.$v['create_time'].";\n";
            }
            $fileName = 'user_up_log_'.date('Y-m-d', TIMESTAMP) .'.csv';
            exportCsv($fileName, $str);
            return;
        }


        $this->load->view('agent/user/up_level', $data);
    }
}
